package com.mmlc.meimei.lichengkongjian.mine;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.androidnetframe.BaseParentActivity;
import com.androidnetframe.httputil.BaseRequestFilterLayer;
import com.androidnetframe.responsejavabean.RequestError;
import com.mmlc.inter.BaseRecyclerAdapter;
import com.mmlc.inter.RecyclerViewHolder;
import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.meimei.lichengkongjian.customview.SwipeRecyclerView;
import com.mmlc.meimei.lichengkongjian.home.LazyFragment;
import com.mmlc.network.filter.ScoreDetailRequestFilter;
import com.mmlc.network.responsebean.ScoreDetailResponseBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/5/22.
 */

public class MyScoreFragment extends LazyFragment implements SwipeRecyclerView.OnLoadListener{

    private BaseRecyclerAdapter<ScoreDetailResponseBean.DataBean> mAdapter;
    private List<ScoreDetailResponseBean.DataBean> mList;
    private SwipeRecyclerView sw_refresh;
    boolean isRefresh = false;
    boolean isLoadMore = false;
    private int type;
    private View rootView;
    private boolean isFirst = true;
    private boolean isPrepared;
    private int pageNow = 0;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if(rootView == null) {
            rootView = inflater.inflate(R.layout.frgment_recycler_view, container, false);
            initView();
            isPrepared = true;
        }
        lazyLoad();
        return rootView;
    }

    protected void initView() {
        type = getArguments().getInt("type");
        mList = new ArrayList<>();

        sw_refresh = (SwipeRecyclerView) rootView.findViewById(R.id.swipeRecyclerView);
        sw_refresh.getSwipeRefreshLayout().setColorSchemeColors(Color.RED);
        GridLayoutManager manager = new GridLayoutManager(getActivity(),1);
        sw_refresh.getRecyclerView().setLayoutManager(manager);
        sw_refresh.setOnLoadListener(this);
        mAdapter = new BaseRecyclerAdapter<ScoreDetailResponseBean.DataBean>(getActivity().getApplicationContext(),mList) {
            @Override
            public int getItemLayoutId(int viewType) {
                return R.layout.adapte_my_score;
            }

            @Override
            public void bindData(RecyclerViewHolder holder, int position, ScoreDetailResponseBean.DataBean item) {
                holder.getTextView(R.id.tv_item_title).setText(item.log);
                holder.getTextView(R.id.tv_item_time).setText(item.add_time);
                holder.getTextView(R.id.tv_item_score).setText(item.score);
            }
        };
        sw_refresh.setAdapter(mAdapter);

    }

    @Override
    protected void lazyLoad() {
        if (!isPrepared || !isVisible || !isFirst) {
            return;
        }
        sw_refresh.post(new Runnable() {
            @Override
            public void run() {
                sw_refresh.setRefreshing(true);
                MyScoreFragment.this.onRefresh();
            }
        });
        isFirst = false;
    }

    @Override
    public void onRefresh() {
        if(!isRefresh){
            isRefresh = true;
            isLoadMore = false;
            pageNow = 0;
            getScoreDetailList();
        }
    }

    @Override
    public void onLoadMore() {
        if(!isLoadMore){
            isLoadMore = true;
            isRefresh = false;
            pageNow++;
            getScoreDetailList();
        }
    }

    private void getScoreDetailList(){
        ScoreDetailRequestFilter filter = new ScoreDetailRequestFilter((BaseParentActivity) getActivity());
        filter.requestBean.type = type;
        filter.requestBean.pageNow = pageNow;
        filter.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<ScoreDetailResponseBean>() {
            @Override
            public void onSuccess(ScoreDetailResponseBean result) {
                sw_refresh.complete();
                isRefresh = false;
                isLoadMore = false;
                if(pageNow == 0){
                    if(mList != null && mList.size() > 0){
                        mList.clear();
                    }
                }
                if(result != null){
                    if(result.data != null && result.data.size() < 20){
                        sw_refresh.setLoadMoreEnable(false);
                    }else if(result.data != null && result.data.size() == 20){
                        sw_refresh.setLoadMoreEnable(true);
                    }
                    mList.addAll(result.data);
                    mAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailed(View errorLayout, RequestError serverError, int failedType) {
                sw_refresh.complete();
                isRefresh = false;
                isLoadMore = false;
            }
        });
    }
}
